package com.example.mybatis.mapper;

import com.example.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    @Select("select id, username, `password`, age, gender, phone, " +
            "delete_flag, create_time, update_time from user_info")
    List<UserInfo> queryAllUser();

    @Results(id = "BasicResult", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
    })
    @Select("select * from user_info")
    List<UserInfo> queryAllUser1();

    @Select("select id, username, `password`, age, gender, phone, " +
            "delete_flag as deleteFlag, create_time as createTime, " +
            "update_time as updateTime from user_info")
    List<UserInfo> queryAllUser2();

    @Select("select * from user_info")
    List<UserInfo> queryAllUser3();

    @Select("select * from user_info where id = #{id}")
    UserInfo queryUserById(Integer id);

    @Select("select * from user_info where id = #{userid}")
    UserInfo queryUserById2(@Param("userid") Integer id);

    @Select("select * from user_info where id = ${id}")
    UserInfo queryUserById3(Integer id);

    @Select("select * from user_info where username = #{userName}")
    UserInfo queryUserByName(String userName);

    @Select("select * from user_info where username = '${userName}'")
    UserInfo queryUserByName2(String userName);

    @Select("select * from user_info where username = ${userName}")
    List<UserInfo> queryUserByName3(String userName);

    @Select("select * from user_info order by id ${order}")
    List<UserInfo> queryUserByOrder(String order);

    @Select("select * from user_info order by id #{order}")
    List<UserInfo> queryUserByOrder1(String order);

    @Select("select * from user_info where username like '%${userName}%'")
    List<UserInfo> queryAllUserByLikeName(String userName);

    @Select("select * from user_info where username like concat('%', #{userName}, '%')")
    List<UserInfo> queryAllUserByLikeName2(String userName);

    @ResultMap(value = "BasicResult")
    @Select("select * from user_info where id = #{id} and delete_flag = #{deleteFlag}")
    UserInfo queryUserInfoByDeleteFlag(Integer id, Integer deleteFlag);

    @Insert("insert into user_info (username, `password`, age, gender, phone)" +
            "values (#{userName}, #{password}, #{age}, #{gender}, #{phone})")
    Integer insert(UserInfo userInfo);

    @Insert("insert into user_info (username, `password`, age, gender, phone)" +
            "values (#{userinfo.userName}, #{userinfo.password}, #{userinfo.age}, #{userinfo.gender}, #{userinfo.phone})")
    Integer insert2(@Param("userinfo") UserInfo userInfo);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username, `password`, age, gender, phone)" +
            "values (#{userName}, #{password}, #{age}, #{gender}, #{phone})")
    Integer insert3(UserInfo userInfo);

    @Update("update user_info set username = #{userName} where id = #{id}")
    Integer update(UserInfo userInfo);

    @Delete("delete from user_info where id = #{id}")
    Integer delete(Integer id);
}
